/*
 * Copyright (C) 2022 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.ynkj.teleprompter.utiles;

import android.content.Context;
import android.os.Build;
import android.util.TypedValue;
import android.widget.Toast;

import java.text.NumberFormat;
import java.util.Random;

public class Utilities {
    public static final boolean ATLEAST_ICE_CREAM;

    public static final boolean ATLEAST_KITKAT;

    public static final boolean ATLEAST_LOLLIPOP;

    static  {
        boolean bool1;
        int i = Build.VERSION.SDK_INT;
        boolean bool2 = true;
        if (i >= 21) {
            bool1 = true;
        } else {
            bool1 = false;
        }
        ATLEAST_LOLLIPOP = bool1;
        if (Build.VERSION.SDK_INT >= 19) {
            bool1 = true;
        } else {
            bool1 = false;
        }
        ATLEAST_KITKAT = bool1;
        if (Build.VERSION.SDK_INT >= 14) {
            bool1 = bool2;
        } else {
            bool1 = false;
        }
        ATLEAST_ICE_CREAM = bool1;
    }

    public static boolean isAtLeastIceCream() { return ATLEAST_ICE_CREAM; }

    public static boolean isAtLeastKK() { return ATLEAST_KITKAT; }

    public static boolean isAtLeastL() { return ATLEAST_LOLLIPOP; }

    public static boolean isEmpty(String str){
        if(str==null||"".equals(str)||str.equals("null")){
            return true;
        }
        return false;
    }

    public static String SubFourPoint2(Object number) {
        if(number!=null){
            Double d = Double.parseDouble(number.toString());
            NumberFormat ddf1=NumberFormat.getNumberInstance();
            ddf1.setMaximumFractionDigits(2);
            String data= ddf1.format(d) ;
            return data;
        }
        return "";
    }

    public  static float dp2Px(float dp,Context context) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics());
    }

    public static void TOAST(Context context, String msg){
        Toast.makeText(context,msg,Toast.LENGTH_SHORT).show();
    }

    /**
     * 生成数字和字母组合，字母区分大小写
     *
     * @param length 随机字符串的长度
     * @return
     */
    public static String generateByRandom(final int length) {
        StringBuilder randomSb = new StringBuilder(length);
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            // 输出字母还是数字
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            // 字符串
            if ("char".equals(charOrNum)) {
                // 判断字母大小写
                int choice = random.nextInt(2) % 2 == 0 ? 65 : 97;
                randomSb = randomSb.append((char) (choice + random.nextInt(26)));
            } else {
                randomSb = randomSb.append(random.nextInt(10));
            }
        }
        return randomSb.toString();
    }
}
